package com.ss.summer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ss.summer.core.exceptions.SummerSearchException;
import com.ss.summer.entity.SummerInstance;
import com.ss.summer.entity.SummerRecord;
import com.ss.summer.mapper.SummerInstanceMapper;
import com.ss.summer.mapper.SummerRecordMapper;
import com.ss.summer.service.SummerSearch;
import com.ss.summer.service.enmus.HasAuthType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 查找服务的实现
 * @Author: 夏十七
 * @Date: 2019/7/31 21:49
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SummerSearchImpl implements SummerSearch {

    @Autowired
    @SuppressWarnings("all")
    private SummerInstanceMapper summerInstanceMapper;
    @Autowired
    @SuppressWarnings("all")
    private SummerRecordMapper summerRecordMapper;


    @Override
    public List<SummerInstance> findWaitAudit(String apiPath, String memberId) {
        return summerInstanceMapper.selectWaitAudit(apiPath,memberId);
    }

    @Override
    public IPage<SummerInstance> findWaitAudit(String apiPath, String memberId, int currentPage, int limit) {
        Page<SummerInstance> page = new Page<>(currentPage - 1, limit);
        return summerInstanceMapper.selectWaitAudit(page,apiPath,memberId);
    }

    @Override
    public List<SummerInstance> findHasAuth(String apiPath, String memberId, HasAuthType type) {
        if (type == null){
            throw new SummerSearchException("type参数不能为null");
        }
        List<SummerInstance> result ;
        switch (type){
            case ALL: result = summerInstanceMapper.selectHasAuth(apiPath,memberId);break;
            case WAIT_AUDIT: result = summerInstanceMapper.selectWaitAudit(apiPath,memberId);break;
            case AUDITED: result = summerInstanceMapper.selectAudited(apiPath,memberId);break;
            case AUDITED_OTHER: result = summerInstanceMapper.selectAuditedForOther(apiPath,memberId);break;
            default:throw new SummerSearchException("未匹配到对应HasAuthType枚举类型");
        }
        return result;
    }

    @Override
    public IPage<SummerInstance> findHasAuth(String apiPath, String memberId, HasAuthType type, int currentPage, int limit) {
        if (type == null){
            throw new SummerSearchException("type参数不能为null");
        }
        Page<SummerInstance> page = new Page<>(currentPage - 1, limit);
        IPage<SummerInstance> result;
        switch (type){
            case ALL: result = summerInstanceMapper.selectHasAuth(page,apiPath,memberId);break;
            case WAIT_AUDIT: result = summerInstanceMapper.selectWaitAudit(page,apiPath,memberId);break;
            case AUDITED: result = summerInstanceMapper.selectAudited(page,apiPath,memberId);break;
            case AUDITED_OTHER: result = summerInstanceMapper.selectAuditedForOther(page,apiPath,memberId);break;
            default:throw new SummerSearchException("未匹配到对应HasAuthType枚举类型");
        }
        return result;
    }


    @Override
    public List<SummerInstance> findAudited(String apiPath, String memberId) {
        return summerInstanceMapper.selectAudited(apiPath,memberId);
    }

    @Override
    public IPage<SummerInstance> findAudited(String apiPath, String memberId, int currentPage, int limit) {
        Page<SummerInstance> page = new Page<>(currentPage - 1, limit);
        return summerInstanceMapper.selectAudited(page,apiPath,memberId);
    }

    @Override
    public List<SummerRecord> findRecord(Integer insId) {
        QueryWrapper<SummerRecord> query = Wrappers.query();
        query.eq("instance_id",insId);
        query.orderByDesc("create_at");
        return summerRecordMapper.selectList(query);
    }

    @Override
    public List<SummerRecord> findRecord(String apiPath, Integer applyId) {
        return summerRecordMapper.selectAllByApiPathAndApplyId(apiPath,applyId);
    }
}
